<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改条文信息')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: select2-css" />
	<th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-clause-edit" th:object="${sysAppClause}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">标题：</label>
                <div class="col-sm-8">
                    <input name="clauseTitle" th:field="*{clauseTitle}" class="form-control" type="text" disabled="true">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">作者：</label>
                <div class="col-sm-8">
                    <input name="author" th:field="*{author}" class="form-control" type="text" disabled="true">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">对应方剂</label>
                <div class="col-sm-8">
	                <select name="prescriptioninfos" id="prescriptioninfos" class="form-control select2-multiple" th:with="type=${@prescription.getInfos(1)}" multiple>
	                    <option th:each="prescription : ${type}" th:text="${prescription.title}" th:value="${prescription.id}"></option>
	                </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/clause";
        // $("#prescriptioninfos").val(['9','10']).select2()
        /* 默认加载时调用 */
        var preId = $("#id").val();
        var arr = []
        	$.ajax({
				type: "post",
				url: prefix + "/selectPrescriptionByPrescriptionId",
				data: {
					prescriptionId:preId
				},
				dataType: 'json',
				success: function(result) {
					if (result.code == web_status.SUCCESS) {
						for(let i in result.rows){
						    console.log(result.rows[i].clauseId)
						    arr.push(result.rows[i].clauseId)
						}
						$("#prescriptioninfos").val(arr).select2()
					} else {
						$.modal.alertWarning("设置失败，请查看失败原因。");
					}
				},
				error: function(error) {
					$.modal.alertWarning("操作失败");
				}
			});
        /*选中时调用*/
        $('#prescriptioninfos').on('select2:select', function (e) {
        	var preId = $("#id").val();
        	$.ajax({
				type: "post",
				url: prefix + "/savePrescription",
				data: {
					clauseId:e.params.data.id,
					prescriptionId:preId
				},
				dataType: 'json',
				success: function(result) {
					if (result.code == web_status.SUCCESS) {
						$.modal.alertWarning("设置成功");
					} else {
						$.modal.alertWarning("设置失败，请查看失败原因。");
					}
				},
				error: function(error) {
					$.modal.alertWarning("操作失败");
				}
			});
    	});
        
    	/* 删除选中时调用 */
    	$('#prescriptioninfos').on('select2:unselect', function (e) {
    		console.log("关闭时运行:"+e.params.data.id)
    		var preId = $("#id").val();
        	$.ajax({
				type: "post",
				url: prefix + "/deletePrescription",
				data: {
					clauseId:e.params.data.id,
					prescriptionId:preId
				},
				dataType: 'json',
				success: function(result) {
					if (result.code == web_status.SUCCESS) {
						$.modal.alertWarning("删除成功");
					} else {
						$.modal.alertWarning("删除失败，请查看失败原因。");
					}
				},
				error: function(error) {
					$.modal.alertWarning("操作失败");
				}
			});
    	});

    </script>
</body>
</html>